package com.example.demo.controller;

import com.example.demo.entity.Wish;
import com.example.demo.exception.IndexException;
import com.example.demo.mapper.WishMapper;
import com.example.demo.response.Response;
import com.example.demo.response.transform.IndexTransform;
import com.example.demo.services.IndexServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class IndexController {

    @Autowired
    WishMapper WishMapper;
    @ResponseBody
    @RequestMapping("/get_wishs")
    public Response getWishs(@RequestParam("uid") int uid){
        List<Wish> wishs = WishMapper.getWishsByUserId(uid);
        return Response.data(new IndexTransform().getWishs(wishs));
    }

    @RequestMapping("/get_wish_detail")
    @ResponseBody
    public Response getWishDetail(@RequestParam("id") int id){
        Wish wish = WishMapper.getWishsById(id);
        return Response.data(new IndexTransform().getWishDetail(wish));
    }

    @Autowired
    IndexServices IndexServices;
    @RequestMapping("/add_wish")
    @ResponseBody
    public Response addWish(@Validated Wish wish, BindingResult bindResult){
        if (bindResult.hasErrors()) {
            throw new IndexException(402,bindResult.getFieldError().getDefaultMessage());
        }
        IndexServices.addWish(wish);
        return Response.success(200,"添加成功");
    }

}
